package com.papper.mng.filter;

import com.google.common.collect.Sets;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.AntPathMatcher;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class FilterConfigTool {
    private final static Logger           logger        = LoggerFactory.getLogger(FilterConfigTool.class);
    private static FilterConfigTool instance      = null;
    /**
     * 过滤-不检验
     */
    private final  Set<String>      exclude       = Sets.newHashSet();
    /**
     * 过滤-渠道允许访问路径
     */
    private final  Set<String>      sourceinclude = Sets.newHashSet();

    public static synchronized FilterConfigTool I() {
        if (instance == null) {
            instance = new FilterConfigTool();
            instance.initPropertys();
        }
        return instance;
    }

    public void initPropertys() {
        try {
            // 加载配置文件
            InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("filter.xml");
            if (fis == null) {
                logger.error("系统过滤配置文件[{}]为空!!!!!!", "filter.xml");
                return;
            }
            InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
            if (isr == null) {
                logger.error("系统过滤配置文件[{}]为空!!!!!!", "filter.xml");
                return;
            }
            SAXReader reader   = new SAXReader();
            Document  document = reader.read(isr);
            List      nodes    = document.getRootElement().elements("props");
            for (Iterator it = nodes.iterator(); it.hasNext(); ) {
                Element elm = (Element) it.next();
                if (elm.attributeValue("name").equals("exclude")) {
                    List props = elm.elements("prop");
                    for (Iterator pit = props.iterator(); pit.hasNext(); ) {
                        Element pelm = (Element) pit.next();
                        exclude.add(pelm.attributeValue("url"));
                    }
                }
                if (elm.attributeValue("name").equals("login")) {
                    List props = elm.elements("prop");
                    for (Iterator pit = props.iterator(); pit.hasNext(); ) {
                        Element pelm = (Element) pit.next();
                        sourceinclude.add(pelm.attributeValue("url"));
                    }
                }
            }
            isr.close();
            fis.close();
        } catch (Exception e) {
            logger.error("系统过滤配置导入异常:[{}]", e.getMessage());
            return;
        }
    }

    public boolean isExclude(String url) {
        return isInclude(url, exclude);
    }

    public boolean isSource(String url) {
        return isInclude(url, sourceinclude);
    }

    /**
     * 是否包含
     *
     * @param url
     * @return
     */
    private boolean isInclude(String url, Set<String> comp) {
        if (url != null) {
            AntPathMatcher path = new AntPathMatcher();
            for (String str : comp) {
                if (path.match(str, url)) {
                    return true;
                }
            }
            return false;
        }
        return false;
    }
}
